* Purpose: To study the intervention strength for info_study
* Author: Sadish
* Last Modified: 2021-04-29
* Notes:

version 13

* Inputs **************************************************************
  local data "${is_root}/6_Processed_Data/is_4_4_6_publicData.dta"
  local effects "${is_root}/7_Output/is_4_4_4_effectsTable"
  local effectsGraph "${is_root}/7_Output/is_4_4_4_effectsGraph"
  local uid "empcode" // variables to uniquely identify individuals
***********************************************************************


clear all
set more off
set mat 10000
set scheme lean1 // plottig
tempfile box


* Get Clean Data ******************************************************
use "`data'", clear
save `box', replace
***********************************************************************


* Graphs **************************************************************
tempfile modelEst
local cond1 ""
local cond2 "& smartphone==0"
local cond3 "& smartphone==1"
local aspect1 = 1.6
local aspect2 = 0.8
local aspect3 = 0.8

forvalues i=1(1)3 {
foreach v in "knowledge" "distress" {
use "`data'", clear

* Graph title
local title: variable label `v'_el

* Control group
cap: est drop est1
reg `v'_el if PC==0 `cond`i''
est sto est1
regsave _cons using `modelEst', replace addlabel(t,0)

* Treatment group
cap: est drop est2
reg `v'_el if PC==1 `cond`i''
est sto est2
regsave _cons using `modelEst', append addlabel(t,1)

* Getting joint significance
suest est1 est2, vce(robust)
test [est1_mean]_cons=[est2_mean]_cons
local pVal=round(r(p),0.01)

* Getting minimum value of dependent variable for graph
su `v'_el
local lo = round(min(r(min),0),0.01)

* Graphing
use `modelEst', clear
keep if var=="_cons"
replace coef=round(coef,0.01)

* Graph parameters
su coef
local hi = round(r(max) + 0.12*(r(max) - `lo'),0.01)
local step=round((`hi'-`lo')/4,0.01)

* rcap parameters
gen lo=0
gen hi=1
su coef
gen rcapPos = r(max) + 0.05*(r(max) - `lo')
local textPos = r(max) + 0.1*(r(max) - `lo')

gr tw (bar coef t, base(`lo')) ///
	  (scatter coef t, m(none) mlab(coef) mlabp(6) mlabs(medium)) ///
	  (rcap lo hi rcapPos, horizontal) ///
	  (scatteri `hi' 0, m(none)) ///
	  , xlabel(0 "Text or Audio" 1 "Live Call") ///
	  xti("") yti("Mean") ylab(`lo'(`step')`hi') ///
	  text(`textPos' 0.5 "p = `pVal'") legend(off) ///
	  aspect(`aspect`i'') title("`title'") ///
	  yscale(extend)
gr save "`effectsGraph'_`v'_`i'.gph", replace
}
}
* Combining graphs
gr combine "`effectsGraph'_knowledge_1.gph" "`effectsGraph'_distress_1.gph", ///
   cols(2) saving("`effectsGraph'.gph", replace)
gr export "`effectsGraph'.png", replace
gr combine "`effectsGraph'_knowledge_2.gph" "`effectsGraph'_distress_2.gph" ///
   "`effectsGraph'_knowledge_3.gph" "`effectsGraph'_distress_3.gph" , ///
   t1title("Without Smartphone") b1title("With Smartphone")
   cols(2) saving("`effectsGraph'_interact.gph", replace)
gr export "`effectsGraph'_interact.png", replace
***********************************************************************


* Regressions *********************************************************
{
local FE "round surveyor_bl surveyor_el"
local X "age female edu_hi quit numeracy memory smartphone hostel"
local vce "robust"
local outcomes "knowledge distress"
local specs = 4 // number of specifications for each outcome

// Tables ***************************************************
*
local heading ""
local counter "Variables"
local b_PC: var lab PC
local se_PC ""
foreach v in `outcomes' {
  local b_`v'_bl: var lab `v'_bl
    local b_`v'_bl = "`b_`v'_bl'" + " (\textit{Bl})"
  local se_`v'_bl ""
}
local sblFE "Surveyor (\textit{Bl}) \textit{FE}"
local selFE "Surveyor (\textit{El}) \textit{FE}"
local rFE "Trial Round \textit{FE}"
local ipw "\textit{IPW}"
local ctrl "Control Variables"
local info "Information Delivery"

local N "Observations"
local ar2 "Adjusted \(R^2\)" // Latex perfers \()\ here rather than $$
local mean "Mean of Outcome (\textit{Bl})"

  local cols=0 // column counter for table
  foreach y in `outcomes' {
  forvalues i=1(1)`specs' {
	// Column counter:
	local cols = `cols' + 1
	local counter "`counter' & (`cols')"
	if `i'==1 {
	  reghdfe `y'_el PC `y'_bl, a(`FE') vce(`vce')
	  
	  local coef_PC = string(_b[PC], "%12.2fc")
	  local b_PC "`b_PC' & `coef_PC'"
	  local err_PC = "(" + string(_se[PC], "%12.2fc") + ")"
	  local se_PC "`se_PC' & `err_PC'"
	  
	  foreach v in `outcomes' {
	  capture: di _b[`v'_bl] // see if variable is regressor
	  if _rc==0 {
	    local coef_bl = string(_b[`v'_bl], "%12.2fc")
		local b_`v'_bl "`b_`v'_bl' & `coef_bl'"
		local err_bl = "(" + string(_se[`v'_bl], "%12.2fc") + ")"
		local se_`v'_bl "`se_`v'_bl' & `err_bl'"
	  }
	  else {
		local b_`v'_bl "`b_`v'_bl' & "
		local se_`v'_bl "`se_`v'_bl' & "
	  }
	  }
	  
	  local sblFE "`sblFE' & \(\checkmark\)"
	  local selFE "`selFE' & \(\checkmark\)"
	  local rFE "`rFE' & \(\checkmark\)"
	  local ipw "`ipw' & "
	  local ctrl "`ctrl' & "
	  local info "`info' & "
	}
	if `i'==2 {
	  reghdfe `y'_el PC `y'_bl [pweight = ipw], a(`FE') vce(`vce')
	  
	  local coef_PC = string(_b[PC], "%12.2fc")
	  local b_PC "`b_PC' & `coef_PC'"
	  local err_PC = "(" + string(_se[PC], "%12.2fc") + ")"
	  local se_PC "`se_PC' & `err_PC'"
	  
	  foreach v in `outcomes' {
	  capture: di _b[`v'_bl] // see if variable is regressor
	  if _rc==0 {
	    local coef_bl = string(_b[`v'_bl], "%12.2fc")
		local b_`v'_bl "`b_`v'_bl' & `coef_bl'"
		local err_bl = "(" + string(_se[`v'_bl], "%12.2fc") + ")"
		local se_`v'_bl "`se_`v'_bl' & `err_bl'"
	  }
	  else {
		local b_`v'_bl "`b_`v'_bl' & "
		local se_`v'_bl "`se_`v'_bl' & "
	  }
	  }
	  
	  local sblFE "`sblFE' & \(\checkmark\)"
	  local selFE "`selFE' & \(\checkmark\)"
	  local rFE "`rFE' & \(\checkmark\)"
	  local ipw "`ipw' & \(\checkmark\)"
	  local ctrl "`ctrl' & "
	  local info "`info' & "
	}
	if `i'==3 {
	  reghdfe `y'_el PC `y'_bl `X' [pweight = ipw], a(`FE') vce(`vce')
	  
	  local coef_PC = string(_b[PC], "%12.2fc")
	  local b_PC "`b_PC' & `coef_PC'"
	  local err_PC = "(" + string(_se[PC], "%12.2fc") + ")"
	  local se_PC "`se_PC' & `err_PC'"
	  
	  foreach v in `outcomes' {
	  capture: di _b[`v'_bl] // see if variable is regressor
	  if _rc==0 {
	    local coef_bl = string(_b[`v'_bl], "%12.2fc")
		local b_`v'_bl "`b_`v'_bl' & `coef_bl'"
		local err_bl = "(" + string(_se[`v'_bl], "%12.2fc") + ")"
		local se_`v'_bl "`se_`v'_bl' & `err_bl'"
	  }
	  else {
		local b_`v'_bl "`b_`v'_bl' & "
		local se_`v'_bl "`se_`v'_bl' & "
	  }
	  }
	  
	  local sblFE "`sblFE' & \(\checkmark\)"
	  local selFE "`selFE' & \(\checkmark\)"
	  local rFE "`rFE' & \(\checkmark\)"
	  local ipw "`ipw' & \(\checkmark\)"
	  local ctrl "`ctrl' & \(\checkmark\)"
	  local info "`info' & "
	}
	if `i'==4 {
	  reghdfe `y'_el PC `y'_bl `X' infoContent infoRepeat [pweight = ipw] ///
	    if (PC==1 | VR==1), a(`FE') vce(`vce')
	  
	  local coef_PC = string(_b[PC], "%12.2fc")
	  local b_PC "`b_PC' & `coef_PC'"
	  local err_PC = "(" + string(_se[PC], "%12.2fc") + ")"
	  local se_PC "`se_PC' & `err_PC'"
	  
	  foreach v in `outcomes' {
	  capture: di _b[`v'_bl] // see if variable is regressor
	  if _rc==0 {
	    local coef_bl = string(_b[`v'_bl], "%12.2fc")
		local b_`v'_bl "`b_`v'_bl' & `coef_bl'"
		local err_bl = "(" + string(_se[`v'_bl], "%12.2fc") + ")"
		local se_`v'_bl "`se_`v'_bl' & `err_bl'"
	  }
	  else {
		local b_`v'_bl "`b_`v'_bl' & "
		local se_`v'_bl "`se_`v'_bl' & "
	  }
	  }
	  
	  local sblFE "`sblFE' & \(\checkmark\)"
	  local selFE "`selFE' & \(\checkmark\)"
	  local rFE "`rFE' & \(\checkmark\)"
	  local ipw "`ipw' & \(\checkmark\)"
	  local ctrl "`ctrl' & \(\checkmark\)"
	  local info "`info' & \(\checkmark\)"
	}

	// Observations:
	local obs = string(e(N), "%12.0fc")
	local N "`N' & `obs'"
	// R squared:
	local adjr2 = string(e(r2_a), "%12.2fc")
	local ar2 "`ar2' & `adjr2'"
  }
  
  // Table headings row:
  local `y'lab: var lab `y'_bl
  local heading "`heading' & \multicolumn{`specs'}{c}{``y'lab'}"
  
  // Mean of Dependent Variable:
  su `y'_bl
  local depmean = string(r(mean), "%12.2fc")
  local mean "`mean' & \multicolumn{`specs'}{c}{`depmean'}"
  }
  // Latex table:
  cap: file close texFile
  file open texFile using "`effects'.tex", write replace
  file write texFile ///
    "\begin{tabular}{a{3.6cm} *{`cols'}{b{1cm}}} \toprule % The *{#} repeats the following argument for # columns. a{} is ragged right column, b{} is centered." _n
  file write texFile " `heading' \\" _n
  file write texFile " `counter' \\ \midrule" _n
  file write texFile " `b_PC' \\" _n
  file write texFile " `se_PC' \\" _n
  foreach v in `outcomes' {
    file write texFile " `b_`v'_bl' \\" _n
    file write texFile " `se_`v'_bl' \\" _n
  }
  file write texFile " `sblFE' \\" _n
  file write texFile " `selFE' \\" _n
  file write texFile " `rFE' \\" _n
  file write texFile " `ipw' \\" _n
  file write texFile " `ctrl' \\" _n
  file write texFile " `info' \\" _n
  file write texFile " `N' \\" _n
  file write texFile " `ar2' \\ % Stata perfers \()\ here rather than $$" _n
  file write texFile " `mean' \\ \bottomrule" _n
  file write texFile "\end{tabular}" _n
  file close texFile
}
//
***********************************************************************

//

* Lee bounds **********************************************************

leebounds knowledge_el PC, tight(knowledge_hi)

leebounds distress_el PC, tight(distress_hi_bl smartphone memory_hi)

leebounds distress_hi_el PC, tight(distress_hi_bl smartphone memory_hi)

***********************************************************************

* Interaction with Caller *********************************************
su who_answered
su info_related
su info_work
su info_unrelated
***********************************************************************


* Supplementary Regressions *******************************************
{
local FE "round surveyor_bl surveyor_el"
local X "age female edu_hi quit numeracy memory smartphone hostel"
local vce "robust"
local outcomes "depression depression_hi anxiety anxiety_hi"
local specs = 1 // number of specifications for each outcome

// Tables ***************************************************
*
local heading ""
local counter "Variables"
local b_PC: var lab PC
local se_PC ""
foreach v in `outcomes' {
  local b_`v'_bl: var lab `v'_bl
    local b_`v'_bl = "`b_`v'_bl'" + " (\textit{Bl})"
  local se_`v'_bl ""
}
local sblFE "Surveyor (\textit{Bl}) \textit{FE}"
local selFE "Surveyor (\textit{El}) \textit{FE}"
local rFE "Trial Round \textit{FE}"
local ipw "\textit{IPW}"
local ctrl "Control Variables"

local N "Observations"
local ar2 "Adjusted \(R^2\)" // Latex perfers \()\ here rather than $$
local mean "Mean of Outcome (\textit{Bl})"

  local cols=0 // column counter for table
  foreach y in `outcomes' {
  forvalues i=1(1)`specs' {
	// Column counter:
	local cols = `cols' + 1
	local counter "`counter' & (`cols')"
	if `i'==1 {
	  reghdfe `y'_el PC `y'_bl `X' [pweight = ipw], a(`FE') vce(`vce')
	  
	  local coef_PC = string(_b[PC], "%12.2fc")
	  local b_PC "`b_PC' & `coef_PC'"
	  local err_PC = "(" + string(_se[PC], "%12.2fc") + ")"
	  local se_PC "`se_PC' & `err_PC'"
	  
	  foreach v in `outcomes' {
	  capture: di _b[`v'_bl] // see if variable is regressor
	  if _rc==0 {
	    local coef_bl = string(_b[`v'_bl], "%12.2fc")
		local b_`v'_bl "`b_`v'_bl' & `coef_bl'"
		local err_bl = "(" + string(_se[`v'_bl], "%12.2fc") + ")"
		local se_`v'_bl "`se_`v'_bl' & `err_bl'"
	  }
	  else {
		local b_`v'_bl "`b_`v'_bl' & "
		local se_`v'_bl "`se_`v'_bl' & "
	  }
	  }
	  
	  local sblFE "`sblFE' & \(\checkmark\)"
	  local selFE "`selFE' & \(\checkmark\)"
	  local rFE "`rFE' & \(\checkmark\)"
	  local ipw "`ipw' & \(\checkmark\)"
	  local ctrl "`ctrl' & \(\checkmark\)"
	}

	// Observations:
	local obs = string(e(N), "%12.0fc")
	local N "`N' & `obs'"
	// R squared:
	local adjr2 = string(e(r2_a), "%12.2fc")
	local ar2 "`ar2' & `adjr2'"
  }
  
  // Table headings row:
  local `y'lab: var lab `y'_bl
  local heading "`heading' & \multicolumn{`specs'}{c}{``y'lab'}"
  
  // Mean of Dependent Variable:
  su `y'_bl
  local depmean = string(r(mean), "%12.2fc")
  local mean "`mean' & \multicolumn{`specs'}{c}{`depmean'}"
  }
  // Latex table:
  cap: file close texFile
  file open texFile using "`effects'_supplementary.tex", write replace
  file write texFile ///
    "\begin{tabular}{a{3.6cm} *{`cols'}{b{1.6cm}}} \toprule % The *{#} repeats the following argument for # columns. a{} is ragged right column, b{} is centered." _n
  file write texFile " `heading' \\" _n
  file write texFile " `counter' \\ \midrule" _n
  file write texFile " `b_PC' \\" _n
  file write texFile " `se_PC' \\" _n
  foreach v in `outcomes' {
    file write texFile " `b_`v'_bl' \\" _n
    file write texFile " `se_`v'_bl' \\" _n
  }
  file write texFile " `sblFE' \\" _n
  file write texFile " `selFE' \\" _n
  file write texFile " `rFE' \\" _n
  file write texFile " `ipw' \\" _n
  file write texFile " `ctrl' \\" _n
  file write texFile " `N' \\" _n
  file write texFile " `ar2' \\ % Stata perfers \()\ here rather than $$" _n
  file write texFile " `mean' \\ \bottomrule" _n
  file write texFile "\end{tabular}" _n
  file close texFile
}
//
***********************************************************************

* Pre-Registered ******************************************************
{
local FE "round surveyor_bl surveyor_el"
local X "age female edu_hi quit numeracy memory smartphone hostel"
local vce "robust"
local outcomes "knowledge knowledge_strong distress"
local specs = 2 // number of specifications for each outcome

// Tables ***************************************************
*
local heading ""
local counter "Variables"
local b_VR: var lab VR
local se_VR ""
local b_PC: var lab PC
local se_PC ""
foreach v in `outcomes' {
  local b_`v'_bl: var lab `v'_bl
    local b_`v'_bl = "`b_`v'_bl'" + " (\textit{Bl})"
  local se_`v'_bl ""
}
local sblFE "Surveyor (\textit{Bl}) \textit{FE}"
local selFE "Surveyor (\textit{El}) \textit{FE}"
local rFE "Trial Round \textit{FE}"
local ipw "\textit{IPW}"
local ctrl "Control Variables"

local N "Observations"
local ar2 "Adjusted \(R^2\)" // Latex perfers \()\ here rather than $$
local mean "Mean of Outcome (\textit{Bl})"

  local cols=0 // column counter for table
  foreach y in `outcomes' {
  forvalues i=1(1)`specs' {
	// Column counter:
	local cols = `cols' + 1
	local counter "`counter' & (`cols')"
	if `i'==1 {
	  reghdfe `y'_el VR PC `y'_bl [pweight = ipw], a(`FE') vce(`vce')
	  
	  local coef_VR = string(_b[VR], "%12.2fc")
	  local b_VR "`b_VR' & `coef_VR'"
	  local err_VR = "(" + string(_se[VR], "%12.2fc") + ")"
	  local se_VR "`se_VR' & `err_VR'"
	  
	  local coef_PC = string(_b[PC], "%12.2fc")
	  local b_PC "`b_PC' & `coef_PC'"
	  local err_PC = "(" + string(_se[PC], "%12.2fc") + ")"
	  local se_PC "`se_PC' & `err_PC'"
	  
	  foreach v in `outcomes' {
	  capture: di _b[`v'_bl] // see if variable is regressor
	  if _rc==0 {
	    local coef_bl = string(_b[`v'_bl], "%12.2fc")
		local b_`v'_bl "`b_`v'_bl' & `coef_bl'"
		local err_bl = "(" + string(_se[`v'_bl], "%12.2fc") + ")"
		local se_`v'_bl "`se_`v'_bl' & `err_bl'"
	  }
	  else {
		local b_`v'_bl "`b_`v'_bl' & "
		local se_`v'_bl "`se_`v'_bl' & "
	  }
	  }
	  
	  local sblFE "`sblFE' & \(\checkmark\)"
	  local selFE "`selFE' & \(\checkmark\)"
	  local rFE "`rFE' & \(\checkmark\)"
	  local ipw "`ipw' & \(\checkmark\)"
	  local ctrl "`ctrl' & "
	}
	if `i'==2 {
	  reghdfe `y'_el VR PC `y'_bl `X' [pweight = ipw], a(`FE') vce(`vce')
	  
	  local coef_VR = string(_b[VR], "%12.2fc")
	  local b_VR "`b_VR' & `coef_VR'"
	  local err_VR = "(" + string(_se[VR], "%12.2fc") + ")"
	  local se_VR "`se_VR' & `err_VR'"
	  
	  local coef_PC = string(_b[PC], "%12.2fc")
	  local b_PC "`b_PC' & `coef_PC'"
	  local err_PC = "(" + string(_se[PC], "%12.2fc") + ")"
	  local se_PC "`se_PC' & `err_PC'"
	  
	  foreach v in `outcomes' {
	  capture: di _b[`v'_bl] // see if variable is regressor
	  if _rc==0 {
	    local coef_bl = string(_b[`v'_bl], "%12.2fc")
		local b_`v'_bl "`b_`v'_bl' & `coef_bl'"
		local err_bl = "(" + string(_se[`v'_bl], "%12.2fc") + ")"
		local se_`v'_bl "`se_`v'_bl' & `err_bl'"
	  }
	  else {
		local b_`v'_bl "`b_`v'_bl' & "
		local se_`v'_bl "`se_`v'_bl' & "
	  }
	  }
	  
	  local sblFE "`sblFE' & \(\checkmark\)"
	  local selFE "`selFE' & \(\checkmark\)"
	  local rFE "`rFE' & \(\checkmark\)"
	  local ipw "`ipw' & \(\checkmark\)"
	  local ctrl "`ctrl' & \(\checkmark\)"
	}

	// Observations:
	local obs = string(e(N), "%12.0fc")
	local N "`N' & `obs'"
	// R squared:
	local adjr2 = string(e(r2_a), "%12.2fc")
	local ar2 "`ar2' & `adjr2'"
  }
  
  // Table headings row:
  local `y'lab: var lab `y'_bl
  local heading "`heading' & \multicolumn{`specs'}{c}{``y'lab'}"
  
  // Mean of Dependent Variable:
  su `y'_bl
  local depmean = string(r(mean), "%12.2fc")
  local mean "`mean' & \multicolumn{`specs'}{c}{`depmean'}"
  }
  // Latex table:
  cap: file close texFile
  file open texFile using "`effects'_prereg.tex", write replace
  file write texFile ///
    "\begin{tabular}{a{3.6cm} *{`cols'}{b{1.2cm}}} \toprule % The *{#} repeats the following argument for # columns. a{} is ragged right column, b{} is centered." _n
  file write texFile " `heading' \\" _n
  file write texFile " `counter' \\ \midrule" _n
  file write texFile " `b_VR' \\" _n
  file write texFile " `se_VR' \\" _n
  file write texFile " `b_PC' \\" _n
  file write texFile " `se_PC' \\" _n
  foreach v in `outcomes' {
    file write texFile " `b_`v'_bl' \\" _n
    file write texFile " `se_`v'_bl' \\" _n
  }
  file write texFile " `sblFE' \\" _n
  file write texFile " `selFE' \\" _n
  file write texFile " `rFE' \\" _n
  file write texFile " `ipw' \\" _n
  file write texFile "`ctrl' \\" _n
  file write texFile " `N' \\" _n
  file write texFile " `ar2' \\ % Stata perfers \()\ here rather than $$" _n
  file write texFile " `mean' \\ \bottomrule" _n
  file write texFile "\end{tabular}" _n
  file close texFile
}
//
***********************************************************************
